﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Collections;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using System.Diagnostics;
using Messenia.Data;
using System.Runtime.Serialization.Formatters.Binary;
using System.Reflection;

namespace Messenia.BE
{
    class Server
    {
        public System.Net.Sockets.TcpListener server;
    
        public Server()
        {
            server = new TcpListener(IPAddress.Any, 3000);
            Thread listenThread = new Thread(new ThreadStart(ListenForClients));
            listenThread.Start();
        }

        private void ListenForClients()
        {
            server.Start();

            while (true)
            {
                //blocks until a client has connected to the server
                TcpClient client = server.AcceptTcpClient();

                //create a thread to handle communication
                //with connected client
                Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
                clientThread.Start(client);
            }
        }

        private void HandleClientComm(object client)
        {
            //TcpClient tcpClient = (TcpClient)client;
            //tcpClient.NoDelay = true;
            //NetworkStream clientStream = tcpClient.GetStream();

            //byte[] message = new byte[4096];
            //int bytesRead;
            //DataPackage p;

            //while (true)
            //{
            //    bytesRead = 0;

            //    try
            //    {
            //        //blocks until a client sends a message
            //        bytesRead = clientStream.Read(message, 0, 4096);
            //        p = getObjectWithByteArray(message) as DataPackage;
            //    }
            //    catch(Exception e)
            //    {
            //        Debug.WriteLine(e.InnerException);
            //        break;
            //    }

            //    if (bytesRead == 0)
            //    {
            //        //the client has disconnected from the server
            //        break;
            //    }

            //    //message has successfully been received
            //    ASCIIEncoding encoder = new ASCIIEncoding();
            //    string inputString = encoder.GetString(message, 0, bytesRead);
            //    if (p.Operation == OperationType.GetAllData)
            //    {

            //        //StreamWriter sw = new StreamWriter(clientStream);
            //        List<projects_list> p_list = new List<projects_list>();
            //        using (MesseniaEntities context = new MesseniaEntities())
            //        {
            //            p_list = (from pl in context.projects_list select pl).ToList();
            //        }
            //        p.Responce = p_list;
            //        byte[] arr = getByteArrayWithObject(p);
            //        clientStream.Write(arr, 0, arr.Length);
            //        clientStream.Flush();
                    

            //        //BinaryFormatter bformatter = new BinaryFormatter();
            //        //bformatter.Serialize(clientStream, p);
            //        //clientStream.Flush();

            //    }
            //}

            //tcpClient.Close();
        }

        public byte[] getByteArrayWithObject(Object o)
        {
            MemoryStream ms = new MemoryStream();
            BinaryFormatter bf1 = new BinaryFormatter();
            bf1.Serialize(ms, o);
            return ms.ToArray();
        }

        private object getObjectWithByteArray(byte[] theByteArray)
        {
            MemoryStream ms = new MemoryStream(theByteArray);
            BinaryFormatter bf1 = new BinaryFormatter();
            ms.Position = 0;
            return bf1.Deserialize(ms);
        }
    }
}
